<?php

class Webmix_AdminCountryController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      		->getNavigation('webmix_admin_main', array(), 'webmix_admin_main_country');
		
      	$this->view->formFilter = $formFilter = new Webmix_Form_Admin_Country_Filter();
	    $page = $this->_getParam('page', 1);
	
	    $table = Engine_Api::_()->getDbtable('countries', 'webmix');
	    $select = $table->select()->where('selected = 1');
	
	    // Process form
	    $values = array();
	    if( $formFilter->isValid($this->_getAllParams()) ) {
	      $values = $formFilter->getValues();
	    }

	    foreach( $values as $key => $value ) {
	      if( null === $value ) {
	        unset($values[$key]);
	      }
	    }
	
	    $values = array_merge(array(
	      'order' => 'country_id',
	      'order_direction' => 'DESC',
	    ), $values);
	    
	    $this->view->assign($values);
	
	    // Set up select info
	    $select->order(( !empty($values['order']) ? $values['order'] : 'country_id' ) . ' ' . ( !empty($values['order_direction']) ? $values['order_direction'] : 'DESC' ));
	
	    if( !empty($values['title']) ) {
	      $select->where('title LIKE ?', '%' . $values['title'] . '%');
	    }
	    if( !empty($values['key']) ) {
	      $select->where('key LIKE ?', '%' . $values['key'] . '%');
	    }

	    if( !empty($values['country_id']) ) {
	      $select->where('country_id = ?', (int) $values['country_id']);
	    }
	    


    // Make paginator
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(150);
    $this->view->paginator = $paginator->setCurrentPageNumber( $page );
    $this->view->formValues = $values;
	}
	public function addAction() {
		$this->_helper->layout->setLayout('admin-simple');
		$form = $this->view->form = new Webmix_Form_Admin_Country_Add ();
		if( !$this->getRequest()->isPost() ) return;	
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues();		
			$db = Engine_Api::_ ()->getDbTable('Countries', 'Webmix')->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$country = Engine_Api::_()->getItem('webmix_country', $values['country']);
				$country->selected = 1;
				$country->save();	

				//add webmix-country
				$webmix = $country->getWebmix();
				//if does not exist webmix-country, create new webmix for this country
				if(!$webmix){
					$viewer = Engine_Api::_()->user()->getViewer();
					$webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
					$row = $webmixTable->createRow();
		            $row->title = $country->title;
		            $row->type = Webmix_Model_Webmix::WEBMIX_TYPE_SYSTEM;
		            $row->owner_id = $viewer->user_id;
		            $row->country_id = $country->getIdentity();
		            $row->save();				
				}
					
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
		
      $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => 1000,
          'parentRefresh'=> 1000,
          'messages' => 'Add Successfully'
      ));
	}
	public function multiModifyAction()
	  {
	    if( $this->getRequest()->isPost() ) {
	      $values = $this->getRequest()->getPost();
	      foreach ($values as $key=>$value) {
	        if( $key == 'modify_' . $value ) {
	          $country = Engine_Api::_()->getItem('webmix_country', (int) $value);
	          if( $values['submit_button'] == 'delete' ) {	            
	              $country->selected = 0;
	   			  $country->save();
	          }
	        }
	      }
	    }	
	    return $this->_helper->redirector->gotoRoute(array('action' => 'index'));
	}
	
  public function deleteAction()
  {
    $id = $this->_getParam('id', null);
    $this->view->country = $country = Engine_Api::_()->getItem('webmix_country', $id);
    $this->view->form = $form = new Webmix_Form_Admin_Country_Delete();
    // deleting user
    //$form->user_id->setValue($id);

    if( $this->getRequest()->isPost() ) {
      $db = Engine_Api::_()->getDbtable('countries', 'webmix')->getAdapter();
      $db->beginTransaction();

      try {
       $country->selected = 0;
	   $country->save();	
        
        $db->commit();
      } catch( Exception $e ) {
        $db->rollBack();
        throw $e;
      }
      
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => true,
        'parentRefresh' => true,
        'format'=> 'smoothbox',
        'messages' => array('This country has been successfully deleted.')
      ));
    }
  }
}
